/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include <string.h>

#include "uartToLoraFrame.h"    // 注意添加头文件路径

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);

void uplink(uint8_t* pBuf, uint8_t bufLen);

volatile uint8_t TxFlag;  // 发送是否完成标记位

/* 数组存放传感器数据，格式根据自己需求设定，此处用固定值进行测试 */
uint8_t datain[MAX_PAYLOAD_LENGTH] = {0x01,0x02,0x03,0x04,0x05,0x00};

uint8_t inlen; /* 传感器数据有效长度，如上面的测试数据即为10 */   

/* 使用数组按照设定的格式存放打包后的传感器数据协议帧，该通过调用与lora连接的串口发送该数组即可实现模块向网关发送数据 */
uint8_t dataout[MAX_FRAME_LENGTH] = {0};

uint8_t outlen; /* 发送数组长度 */

uint8_t count = 0;  // count用于计数光线传感器被触发的次数


int main(void)
{

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
/* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();  // GPIO和串口初始化
  MX_USART2_UART_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();
 
  inlen = 6;

  while (1)
  {

    datain[5] = count;  //  更改传感器原始数据
//	datain[5] = 0xff;
		/* USER CODE END WHILE */

    genMsgFrame(datain, &inlen, dataout, &outlen);  // 生成串口协议帧

    uplink(dataout, outlen);  // 发送串口协议帧

    HAL_Delay(5000);  // 延时5s

  }

}

/* 串口向LoRa模块发送数据函数 */
void uplink(uint8_t* pBuf, uint8_t bufLen)
{
    HAL_GPIO_WritePin(SETA_GPIO_Port, SETA_Pin, GPIO_PIN_RESET);
		HAL_Delay(50);

		HAL_UART_Transmit_IT(&huart1, (uint8_t*)pBuf, bufLen);		
		while(TxFlag != 1);
		TxFlag = 0;

    HAL_Delay(50);
		HAL_GPIO_WritePin(SETA_GPIO_Port, SETA_Pin, GPIO_PIN_SET);
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == LightEvent_Pin)
	{
//		flag = (HAL_GPIO_ReadPin(LightEvent_GPIO_Port, LightEvent_Pin) == GPIO_PIN_SET)? 0 : 1;
      count += 1; // 改变光照触发中断，count计数加一
	}	
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	TxFlag = 1;
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/**
  * @brief NVIC Configuration.
  * @retval None
  */
static void MX_NVIC_Init(void)
{
  /* EXTI9_5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  /* USART1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USART2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* EXTI15_10_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
